package com.lovo.pz.window;

import com.lovo.pz.role.*;
import com.lovo.pz.util.FileUtil;
import com.lovo.pz.util.GameControl;
import com.lovo.pz.util.GameMusic;
import com.lovo.pz.util.GameUtil;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 游戏面板
 */
public class GamePanel extends JPanel implements Runnable {
    private MainFrame mainFrame;
    private Font font = new Font("黑体",Font.BOLD,25);
    private MapRole mapRole;
    /** 植物面板角色 */
    private PlantPanelRole plantPanelRole;
    /** 铲子角色 */
    private ShovelRole shovelRole;
    /** 关卡角色 */
    private ProgressRole progressRole;
    /** 被选中的植物对象 */
    private PlantRole selectedPlant;
    /** 投射到草地上的植物阴影对象 */
    private PlantRole tempPlant;
    /** 所有安放在草地上的植物对象集合 */
    private List<PlantRole> plantRoleList;
    /** 除草车集合 */
    private List<CarRole> carRoleList;
    /** 植物卡片集合 */
    private List<PlantCardRole> plantCardRoleList;
    /** 太阳种子集合 */
    private List<SunSeedRole> sunSeedRoleList;
    /** 豌豆射手子弹集合*/
    private List<PeaShooterBulletRole> peaShooterBulletRoleList;
    /** 僵尸角色的集合 */
    private List<ZombieRole> zombieRoleList;
    /** 游戏状态 */
    private int gameStatus;

    public GamePanel(MainFrame mainFrame){
        this.mainFrame = mainFrame;
        GameControl gameControl = new GameControl(this);
        this.addMouseListener(gameControl);
        this.addMouseMotionListener(gameControl);
//        startGame();
    }

    // 初始化角色对象
    public void init(){
        mapRole = new MapRole(this);
        plantPanelRole = new PlantPanelRole(this);
        shovelRole = new ShovelRole(this);
        progressRole = new ProgressRole(this);
        // 植物卡片对象集合
        plantCardRoleList = new ArrayList<>();
        /** 太阳种子对象集合 */
        sunSeedRoleList = new ArrayList<>();
        /** 植物对象集合 */
        plantRoleList = new ArrayList<>();
        // 豌豆射手子弹集合
        peaShooterBulletRoleList = new ArrayList<>();
        // 僵尸角色集合
        zombieRoleList = new ArrayList<>();
        // 创建豌豆射手卡片对象
        PlantCardRole peaShooterCard = new PlantCardRole(this);
        peaShooterCard.setRoleX(220);
        peaShooterCard.setRoleY(9);
        peaShooterCard.setCost(100);
        peaShooterCard.setPlantType(PlantCardRole.PEA_SHOOTER);
        plantCardRoleList.add(peaShooterCard);
        // 创建太阳花卡片对象
        PlantCardRole sunFlowerCard = new PlantCardRole(this);
        sunFlowerCard.setRoleX(272);
        sunFlowerCard.setRoleY(9);
        sunFlowerCard.setCost(50);
        sunFlowerCard.setPlantType(PlantCardRole.SUN_FLOWER);
        plantCardRoleList.add(sunFlowerCard);

        // 产生除草车对象
        carRoleList = new ArrayList<>();
        CarRole carRole = new CarRole(this);
        carRole.setRoleY(110);
        carRoleList.add(carRole);

        carRole = new CarRole(this);
        carRole.setRoleY(202);
        carRoleList.add(carRole);

        carRole = new CarRole(this);
        carRole.setRoleY(294);
        carRoleList.add(carRole);

        carRole = new CarRole(this);
        carRole.setRoleY(386);
        carRoleList.add(carRole);

        carRole = new CarRole(this);
        carRole.setRoleY(478);
        carRoleList.add(carRole);
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        mapRole.paintRole(g);
        plantPanelRole.paintRole(g);
        progressRole.paintRole(g);
        shovelRole.paintRole(g);

        // 绘制植物卡片
        for(int i = 0;i < plantCardRoleList.size();i++){
            plantCardRoleList.get(i).paintRole(g);
        }

        // 绘制植物
        for(int i = 0;i < plantRoleList.size();i++){
            plantRoleList.get(i).paintRole(g);
        }

        // 绘制植物阴影
        if(tempPlant != null){
            tempPlant.paintRole(g);
        }

        // 绘制选中植物
        if(selectedPlant != null){
            selectedPlant.paintRole(g);
        }

        // 绘制僵尸
        for(int i = 0;i < zombieRoleList.size();i++){
            zombieRoleList.get(i).paintRole(g);
        }

        // 绘制豌豆射手的子弹
        for(int i = 0;i < peaShooterBulletRoleList.size();i++){
            peaShooterBulletRoleList.get(i).paintRole(g);
        }

        // 绘制除草车
        for (int i = 0;i < carRoleList.size();i++){
            carRoleList.get(i).paintRole(g);
        }
        // 绘制太阳种子
        for(int i = 0;i < sunSeedRoleList.size();i++){
            sunSeedRoleList.get(i).paintRole(g);
        }

        g.setFont(font);
        g.setColor(Color.RED);
        g.drawString("玩家姓名：" + mainFrame.getUserBean().getName()
                    + " 玩家分数：" + mainFrame.getUserBean().getScore(),160,595);

        // 绘制准备图片
        if(gameStatus == BaseRole.GAME_READY){
            g.drawImage(GameUtil.startReadyImage,300,300,this);
        }
        // 绘制安放图片
        if(gameStatus == BaseRole.GAME_SET){
            g.drawImage(GameUtil.startSetImage,300,300,this);
        }
        // 绘制植物图片
        if(gameStatus == BaseRole.GAME_PLANT){
            g.drawImage(GameUtil.startPlantImage,300,300,this);
        }
        // 绘制僵尸胜利的图片
        if(gameStatus == BaseRole.GAME_ZOMBIE_WON){
            g.drawImage(GameUtil.zombiesWonImage,100,100,this);
        }
    }

    /**
     * 开始游戏
     */
    public void startGame(){
        GameMusic.stopLoopMusic();
        GameMusic.playLoopMusic(GameUtil.mainMusic);
        init();
        gameStatus = BaseRole.GAME_START;
        Thread th = new Thread(this);
        th.start();
    }

    @Override
    public void run() {
        gameStatus = BaseRole.GAME_READY;
        this.repaint();
        GameUtil.sleep(1000);
        gameStatus = BaseRole.GAME_SET;
        this.repaint();
        GameUtil.sleep(1000);
        gameStatus = BaseRole.GAME_PLANT;
        this.repaint();
        GameUtil.sleep(2000);
        gameStatus = BaseRole.GAME_START;
        this.repaint();

        // 太阳种子落下
        GameUtil.generateSunSeed(this);
        // 僵尸产生
        GameUtil.generateZombie(this);
        while(gameStatus == BaseRole.GAME_START){
            progressRole.action();
            for(int i = carRoleList.size() - 1;i >= 0;i--){
                carRoleList.get(i).action();
                if(carRoleList.get(i).getRunStatus() == BaseRole.ROLE_END){
                    carRoleList.remove(i);
                }
            }
            for(int i = plantCardRoleList.size() - 1;i >= 0;i--){
                plantCardRoleList.get(i).action();
            }
            for(int i = peaShooterBulletRoleList.size() - 1;i >= 0;i--){
                peaShooterBulletRoleList.get(i).action();
                if(peaShooterBulletRoleList.get(i).getRunStatus() == BaseRole.ROLE_END){
                    peaShooterBulletRoleList.remove(i);
                }
            }
            for(int i = sunSeedRoleList.size() - 1;i >= 0;i--){
                sunSeedRoleList.get(i).action();
                if(sunSeedRoleList.get(i).getRunStatus() == BaseRole.ROLE_END){
                    sunSeedRoleList.remove(i);
                }
            }
            for(int i = plantRoleList.size() - 1;i >= 0;i--){
                plantRoleList.get(i).action();
                if(plantRoleList.get(i).getRunStatus() == BaseRole.ROLE_END){
                    plantRoleList.remove(i);
                }
            }
            for(int i = zombieRoleList.size() - 1;i >= 0;i--){
                zombieRoleList.get(i).action();
                if(zombieRoleList.get(i).getRunStatus() == BaseRole.ROLE_END){
                    zombieRoleList.remove(i);
                }
            }
            this.repaint();
            try {
                Thread.sleep(15);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if(gameStatus == BaseRole.GAME_ZOMBIE_WON){
            GameUtil.sleep(5000);
        }
        // 将玩家的集合写入到文件
        FileUtil.write(mainFrame.getUsers());
        mainFrame.switchPanel("menu");
    }

    public List<SunSeedRole> getSunSeedRoleList() {
        return sunSeedRoleList;
    }

    public void setSunSeedRoleList(List<SunSeedRole> sunSeedRoleList) {
        this.sunSeedRoleList = sunSeedRoleList;
    }

    public int getGameStatus() {
        return gameStatus;
    }

    public void setGameStatus(int gameStatus) {
        this.gameStatus = gameStatus;
    }

    public PlantPanelRole getPlantPanelRole() {
        return plantPanelRole;
    }

    public void setPlantPanelRole(PlantPanelRole plantPanelRole) {
        this.plantPanelRole = plantPanelRole;
    }

    public ShovelRole getShovelRole() {
        return shovelRole;
    }

    public void setShovelRole(ShovelRole shovelRole) {
        this.shovelRole = shovelRole;
    }

    public PlantRole getSelectedPlant() {
        return selectedPlant;
    }

    public void setSelectedPlant(PlantRole selectedPlant) {
        this.selectedPlant = selectedPlant;
    }

    public PlantRole getTempPlant() {
        return tempPlant;
    }

    public void setTempPlant(PlantRole tempPlant) {
        this.tempPlant = tempPlant;
    }

    public List<PlantRole> getPlantRoleList() {
        return plantRoleList;
    }

    public void setPlantRoleList(List<PlantRole> plantRoleList) {
        this.plantRoleList = plantRoleList;
    }

    public List<PlantCardRole> getPlantCardRoleList() {
        return plantCardRoleList;
    }

    public void setPlantCardRoleList(List<PlantCardRole> plantCardRoleList) {
        this.plantCardRoleList = plantCardRoleList;
    }

    public List<PeaShooterBulletRole> getPeaShooterBulletRoleList() {
        return peaShooterBulletRoleList;
    }

    public void setPeaShooterBulletRoleList(List<PeaShooterBulletRole> peaShooterBulletRoleList) {
        this.peaShooterBulletRoleList = peaShooterBulletRoleList;
    }

    public MainFrame getMainFrame() {
        return mainFrame;
    }

    public void setMainFrame(MainFrame mainFrame) {
        this.mainFrame = mainFrame;
    }

    public List<ZombieRole> getZombieRoleList() {
        return zombieRoleList;
    }

    public void setZombieRoleList(List<ZombieRole> zombieRoleList) {
        this.zombieRoleList = zombieRoleList;
    }
}
